%% it produces Figure 5 a-b, Figure 6 a-b, it computes the percent increase in average employment after the subsidy

adj_fact = A/(A/nu-chi);

b_int=b_vec_d(1:inde_bi(end)+1);
pp_v=spline(b_int,v_new(1:inde_bi(end)+1));
pp_x=spline(b_int,x(1:inde_bi(end)+1));

sub_vec = [0.001,0.01:0.01:0.2];Delta_b=[];ff=[];dm1=0;
b_opt=zeros(length(b_int),length(sub_vec));
for ss=1:length(sub_vec)
    del = sub_vec(ss)*k/(z_0*A_s);
    sub_size_vec(ss)=del;
    for ii=1:length(b_int)
        b   = b_int(ii);
        %compute maximum reduction in stock of debt
        [Delta_b(ii,ss),fv2(ii,ss)]= fsolve(@(d) del-d.*max(0,min(ppval(pp_x,b-d),1)),del*0.7);
        dm1=Delta_b(ii,ss);
        if lambda(ii)>0
            b_opt(ii,ss) = fun_buyback(b_int(ii),A_s,del,cub_spl_v,cub_spl_x );
        else
            b_opt(ii,ss)= b_int(ii);
        end
        if abs(fv2(ii,ss))>1/100,Delta_b(ii,ss)=0;end
    end
end

%frac_k_expost= sub_size*sub_time;
frac_k_expost= 29000/k20;
inde_sub     = find(sub_vec>=frac_k_expost,1);
inde_b_star  = find(lambda>0,1)-1;

del     =  frac_k_expost*k/(z_0*A_s);
bup_opt = fun_buyback(b_up,A_s,del,cub_spl_v,cub_spl_x );
frac_to_reduce_debt=(ppval(pp_x,bup_opt).*(bup_opt-b_up))/(-del)
reg_coef = frac_to_reduce_debt*q_hig_s
%increase in investment
delta_invest_expe= log(ppval(pp_inv,bup_opt)/ppval(pp_inv,b_up))
delta_invest_drift= log(ppval(pp_drift,bup_opt)/ppval(pp_drift,b_up))
inde_bup=find(b_int>b_up,1);

figure(200)
plot(adj_fact*b_int(inde_bi(2:end)),100*ppval(pp_x,b_opt(inde_bi(2:end),inde_sub)').*(b_opt(inde_bi(2:end),inde_sub)'-b_int(inde_bi(2:end)))./(-sub_size_vec(inde_sub)),'-b','LineWidth',6)
xline(ba(inde_const),'--k','FC binding: $b^*$','Interpreter','Latex','Fontsize',18)
xline(adj_fact*b_up,'r--',{'High leverage entrant'},'LineWidth',2,'FontSize',18,'Interpreter','Latex','LabelVerticalAlignment','bottom');
xline(adj_fact*b_md,'r--',{'Low leverage entrant'},'LineWidth',2,'FontSize',18,'Interpreter','Latex','LabelVerticalAlignment','bottom');
grid on
%set(le,'Interpreter','Latex','Fontsize',16);
xlabel('Leverage $b$','Fontsize',24,'Interpreter','Latex')
ylabel('\% of subsidy used to reduce $b$','Fontsize',24,'Interpreter','Latex')
settt
grid on
str1="/figures/figure_rat.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)

figure(201)
plot(adj_fact*b_int(inde_bi(2:end)),-adj_fact*(b_opt(inde_bi(2:end),inde_sub)'-b_int(inde_bi(2:end))) ,'-b','LineWidth',6),hold on
plot(adj_fact*b_int(inde_bi(inde_b_star:end)),adj_fact*(b_int(inde_bi(inde_b_star:end))-b_int(inde_b_star)),'r-.','LineWidth',6);
plot(adj_fact*b_int(inde_bi(1:end)),adj_fact*Delta_b(inde_bi(1:end),inde_sub),'k--','LineWidth',6);hold off
le=legend('Optimal reduction in $b$, $\Delta$','Reduction in $b$ to achieve $b^*$, $\Delta^{*}$','Max reduction in $b$, $\bar\Delta$');
set(le,'Interpreter','Latex','Fontsize',16,'Location','NorthWest');
grid on
xlabel('Leverage $b$','Fontsize',24,'Interpreter','Latex')
ylabel('Change in $b$','Fontsize',24,'Interpreter','Latex')
settt
grid on
str1="/figures/figure_rat0.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)

figure(202)
plot(adj_fact*Delta_b(inde_bup,:),100*ppval(pp_x,b_opt(inde_bup,:)').*(b_opt(inde_bup,:)'-b_int(inde_bup))./(-sub_size_vec'),'-b','LineWidth',6)
grid on
%set(le,'Interpreter','Latex','Fontsize',16);
xlabel('Max reduction in $b$, $\bar\Delta$','Fontsize',24,'Interpreter','Latex')
ylabel('\% of subsidy used to reduce $b$','Fontsize',24,'Interpreter','Latex')
settt
grid on
str1="/figures/figure_rat2.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)

figure(203)
plot(adj_fact*Delta_b(inde_bup,:),-adj_fact*(b_opt(inde_bup,:)'-b_int(inde_bup)),'-b','LineWidth',6),hold on
plot(adj_fact*Delta_b(inde_bup,:),adj_fact*(b_int(inde_bup)-b_int(inde_b_star))*ones(length(Delta_b(inde_bup,:)),1),'--r','LineWidth',6),hold off
grid on
%yline(adj_fact*(b_int(inde_bup)-b_int(inde_b_star)),'r--',{'Reduction in b to achieve $b^*$'},'LineWidth',6,'FontSize',18,'Interpreter','Latex');
le=legend('Optimal reduction in $b$, $\Delta$','Reduction in b to achieve $b^*$, $\Delta^*$');
set(le,'Interpreter','Latex','Fontsize',16,'Location','southeast');
xlabel('Max reduction in $b$, $\bar\Delta$','Fontsize',24,'Interpreter','Latex')
ylabel('Change in $b$','Fontsize',24,'Interpreter','Latex')
settt
grid on
str1="/figures/figure_rat3.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)

%% investment sensitivity to transfer
frac_va      = 1/k;
inde_sub     = find(sub_vec>=frac_va,1);


%% change in employment
b_up0=b_up;
b_md0=b_md;
zlow0=zlow;
zmid0=zmid;

d_employment=1;
if d_employment==1
    T=1/dt;n_sim=100000;
    b_up=b_up0;
    b_md=b_md0;
    zlow=zlow0*z_0*(1-1/nu)^nu*w_s^(-nu)*Y;%initial number of workers of firm with low productivity
    zmid=zmid0*z_0*(1-1/nu)^nu*w_s^(-nu)*Y;%initial number of workers of firm with high productivity
    get_firm_simulations;
    z_all_0=z_all;

    b_up=b_up0-frac_to_reduce_debt*frac_k_expost*k/(z_0*A_s_ben);
    b_md=b_md0;
    get_firm_simulations;
    z_all_1=z_all;

    dlog_employment = log(z_all_1/z_all_0)

    %compute emplyment effect
    %stats
    wb= (1-1/nu)*(A+ppval(pp_drift,b_up).^(1+theta)/(1+theta)/zeta^theta)
    wb_const=(ppval(pp_drift,b_up).^(1+theta)/(1+theta)/zeta^theta)/(A+ppval(pp_drift,b_up).^(1+theta)/(1+theta)/zeta^theta)
    EdZ = dlog_employment*q_hig;
    Edi = delta_invest_drift*q_hig;
    %Edwb= wb_const*(1+theta)*(Edi) + EdZ
    Edwb= wb_const*delta_invest_expe*q_hig + EdZ

end

